import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:fluttertoast/fluttertoast.dart';

enum LoadState { State_Success, State_Error, State_Loading, State_Empty }

class LoadStateLayout extends StatefulWidget {
  LoadState state; //页面视图
  final Widget? successWidget; //成功视图
  final VoidCallback? errorRetry;
  String? errorMsg;

  LoadStateLayout(
      {Key? key,
      this.state = LoadState.State_Loading,
      this.successWidget,
      this.errorMsg,
      this.errorRetry})
      : super(key: key);

  @override
  State<StatefulWidget> createState() => _LoadStateLayoutState();

}

class _LoadStateLayoutState extends State<LoadStateLayout> {

  update(LoadState loadState){
    setState(() {
      widget.state = loadState;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      //宽高都充满屏幕剩余空间
      width: double.infinity,
      height: double.infinity,
      child: _buildWidget,
    );
  }

  ///根据不同状态来显示不同的视图
  Widget get _buildWidget{
    switch(widget.state) {
      case LoadState.State_Success:
        return widget.successWidget ?? const SizedBox();
      case LoadState.State_Loading:
        return _loadingView;
      case LoadState.State_Error:
        return _errorView;
      case LoadState.State_Empty:
        return _emptyView;
      default:
        return _loadingView;
    }
  }

  Widget get _emptyView{
    return Container(
      width: double.infinity,
      height: double.infinity,
      alignment: Alignment.center,
      padding: const EdgeInsets.only(bottom: 30),
      child: Column(
        mainAxisSize: MainAxisSize.max,
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Image.asset('lib/res/assets/mine_bg.png'),
          const Text('i am empty view')
        ],
      ),
    );
  }

  Widget get _errorView{
    return Container(
      width: double.infinity,
      height: double.infinity,
      alignment: Alignment.center,
      padding: const EdgeInsets.only(bottom: 30),
      child: GestureDetector(
        onTap: () {
          // Fluttertoast.showToast(msg: 'retry');
          if(widget.errorRetry!=null) {
            widget.errorRetry!();
          }
        },
        child: Column(
          mainAxisSize: MainAxisSize.max,
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Image.asset('lib/res/assets/a.jpg'),
            Text(widget.errorMsg ?? 'i am a error View')
          ],
        ),
      ),
    );
  }

  //加载中视图
  Widget get _loadingView{
    return Container(
      width: double.infinity,
      height: double.infinity,
      color: Colors.amberAccent,
      child: Column(
        mainAxisSize: MainAxisSize.max,  // MainAxisSize.min：高度与子控件保持一致
        mainAxisAlignment: MainAxisAlignment.center, // 主轴对齐方式，就是Column中子控件在垂直方向的对齐方式
        crossAxisAlignment: CrossAxisAlignment.center, //交叉轴对齐方式，就是Column中子控件在水平方向的对齐方式，所谓的交叉轴就是与主轴垂直的方向
        children: const [
          CircularProgressIndicator(
            strokeWidth: 3,
            valueColor: AlwaysStoppedAnimation(Colors.blue),
          ),
          Text('loading...'),
        ],
      ),
    );
  }


}
